package cn.openea.perpartner.space.web.controller;


import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.space.web.entity.ShareLog;
import cn.openea.perpartner.space.web.service.IShareLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 邀请记录表(ShareLog)表控制层
 *
 * @author Weird077
 * @since 2023-03-22 15:19:21
 */
@Api(tags = "海上新空间")
@RestController
@RequestMapping("/space/share")
public class ShareController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private IShareLogService shareLogService;

    @ApiOperation(value = "生成邀请码")
    @PostMapping("/create")
    public AjaxResult create(@RequestBody ShareLog shareLog){
        String shareId = shareLogService.checkShareIdUsed(shareLog.getUserId());
        if (StrUtil.isNotBlank(shareId)) {
            return error("邀请码("+shareId+")还未使用，无法生成新的邀请码！");
        }
        return success(shareLogService.saveShareLog(shareLog));
    }

    @ApiOperation(value = "未使用邀请码")
    @GetMapping("/code/{userId}")
    public AjaxResult selectLatest(@PathVariable String userId){
        String shareId = shareLogService.checkShareIdUsed(userId);
        return StrUtil.isBlank(shareId)?error("请生成新的邀请码！"):AjaxResult.success("获取邀请码成功！",shareId);
    }


}

